// user_pkg/pages/addaddress/addaddress.js
Page({
  data: {
    regionList: {},
    multiIndex: [],
    newMultiIndex: [0, 0, 0],
    nameTip: '',
    phoneTip: '',
    regionTip: '',
    detailTip: '',
    addressId: '',
    addressDetail: {}
  },
  onLoad(query) {
    this.setData({
      addressId: query.addressId
    })
    this.getAddressDetail()
  },
  onShow() {
    this.getRegionList()
  },
  async onReady() {
    await this.getRegionList()
    if(this.data.addressId) {
      this.getPickerValue()
    }
    const regionList = this.data.regionList
    const multiIndex = this.data.multiIndex

    // 初始化多列选择器数据
    let multiArray = [
      regionList,
      regionList[0].city,
      regionList[0].city[0].region
    ]
    let region = [
      {
        value: regionList[0].id,
        label: regionList[0].name
      },
      {
        value: regionList[0].city[0].id,
        label: regionList[0].city[0].name
      },
      {
        value: regionList[0].city[0].region[0].id,
        label: regionList[0].city[0].region[0].name
      }
    ]

    if(this.data.addressId) {
      this.setData({newMultiIndex: multiIndex})
      multiArray = [
        regionList,
        regionList[multiIndex[0]].city,
        regionList[multiIndex[0]].city[multiIndex[1]].region
      ]
      region = [
        {
          value: regionList[multiIndex[0]].id,
          label: regionList[multiIndex[0]].name
        },
        {
          value: regionList[multiIndex[0]].city[multiIndex[1]].id,
          label: regionList[multiIndex[0]].city[multiIndex[1]].name
        },
        {
          value: regionList[multiIndex[0]].city[multiIndex[1]].region[multiIndex[2]].id,
          label: regionList[multiIndex[0]].city[multiIndex[1]].region[multiIndex[2]].name
        }
      ]
    }

    this.setData({
      multiArray,
      region
    })
  },
  // 获取省市区列表
  async getRegionList() {
    const res = await wx.http.get('/region/tree')
    const regionList = res.data.data.list
    // 修改省市区列表样式
    let Obj = regionList
    let Arr = []
    for(let key in Obj) {
      Arr.push(Obj[key])
    }
    Arr.map(item => {
      const newObj = item.city
      const newArr = []
      for(let key in newObj) {
        newArr.push(newObj[key])
      }
      item.city = newArr
      item.city.map(newItem => {
        const newNewObj = newItem.region
        const newNewArr = []
        for(let key in newNewObj) {
          newNewArr.push(newNewObj[key])
        }
        newItem.region = newNewArr
      })
    })
    this.setData({regionList: Arr})
  },
  // 多列选择器逻辑
  // 获取多列的索引
  multiChange(e) {
    const multiIndex = e.detail.value
    this.setData({multiIndex})
  },
  // 选择列
  columnChange(e) {
    const regionList = this.data.regionList
    let multiIndex = this.data.newMultiIndex
    const columnIndex = e.detail.column
    const columnValue = e.detail.value

    multiIndex[columnIndex] = columnValue
    this.setData({newMultiIndex: multiIndex, multiIndex})

    this.setData({
      multiArray: [
        regionList,
        regionList[multiIndex[0]].city,
        regionList[multiIndex[0]].city[multiIndex[1]].region
      ],
      region: [
        {
          value: regionList[multiIndex[0]].id,
          label: regionList[multiIndex[0]].name
        },
        {
          value: regionList[multiIndex[0]].city[multiIndex[1]].id,
          label: regionList[multiIndex[0]].city[multiIndex[1]].name
        },
        {
          value: regionList[multiIndex[0]].city[multiIndex[1]].region[multiIndex[2]].id,
          label: regionList[multiIndex[0]].city[multiIndex[1]].region[multiIndex[2]].name
        }
      ]
    })
  },
  // 提交表单
  async formSubmit(ev) {
    let form = ev.detail.value
    form.region = this.data.region
    const phoneRule = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1589]))\d{8}$/
    if(form.name === '') {
      this.setData({nameTip: '收货人姓名不允许为空'})
    }
    if(form.phone === '') {
      this.setData({phoneTip: '手机号码不允许为空'})
    }
    if(!phoneRule.test(form.phone)) {
      this.setData({phoneTip: '请输入正确的手机号码'})
    }
    if(this.data.multiIndex.length === 0) {
      this.setData({regionTip: '请选择地区'})
    }
    if(form.detail === '') {
      this.setData({detailTip: '详细地址不允许为空'})
    }
    if(form.name !== '' && form.phone !== '' && phoneRule.test(form.phone) && this.data.multiIndex.length !== 0 && form.detail !== '') {
      this.setData({
        nameTip: '',
        phoneTip: '',
        regionTip: '',
        detailTip: ''
      })
    }
    if(this.data.addressId) {
      await wx.http.post('/address/edit', {addressId: this.data.addressId, form})
    } else {
      await wx.http.post('/address/add', {form})
    }
    wx.navigateBack({
      delta: 1
    })
  },
  // 编辑状态
  // 获取收货地址详情
  async getAddressDetail() {
    const res = await wx.http.get('/address/detail', {
      addressId: this.data.addressId
    })
    const addressDetail = res.data.data.detail
    this.setData({addressDetail})
  },
  // 根据收货地址详情的省市区ID获取picker对应value
  getPickerValue() {
    const addressDetail = this.data.addressDetail
    const regionList = this.data.regionList
    const provinceId = addressDetail.province_id
    const cityId = addressDetail.city_id
    const regionId = addressDetail.region_id
    let provinceIndex = 0
    let provinceList = []
    let cityIndex = 0
    let cityList = []
    let regionIndex = 0
    // 获取省份索引号
    for(let i = 0; i < regionList.length; i++) {
      if(regionList[i].id === provinceId) {
        provinceIndex = i
        provinceList = regionList[i].city
        break
      }
    }
    // 获取城市索引号
    for(let i = 0; i < provinceList.length; i++) {
      if(provinceList[i].id === cityId) {
        cityIndex = i
        cityList = provinceList[i].region
      }
    }
    // 获取地区索引号
    for(let i = 0; i < cityList.length; i++) {
      if(cityList[i].id === regionId) {
        regionIndex = i
      }
    }
    const multiIndex = [provinceIndex, cityIndex, regionIndex]
    this.setData({multiIndex})
  }
})